﻿var g_vXiaoChuanStrs = [];
var g_vXiaoChuanXmlDoc;
function OnXiaoChuanFlashReady(id) {
    //setTimeout("loadXiaoChuanFlashData('"+id+"')",1000);
    loadXiaoChuanFlashData(id);
}
function getXiaoChuanFlash() {
    if (typeof (g_vIndicatorFlash) == "undefined") {
        g_vXiaoChuanFlash = "xiaochuanchart";
    }
    return g_vXiaoChuanFlash;
}
function getDegreeDesc(datavalue) {
    var YAxis = [0, 15, 18, 24, 25];
    var YAxisLabels = ["没有控制", "部分控制", "部分控制", "部分控制", "完全控制"];
    return YAxisLabels[Math.floor(getValueByArray(YAxis, datavalue))];

}
function getDegreeDescRange(index) {
    return ["没有控制", "部分控制", "部分控制", "部分控制", "完全控制"][Math.floor(index)];
}


function printData(iPageNo, iPageNum, iTotal) {
    var oParent = _$("XiaoChuan_list");
    if (oParent == null) return;
    var xResult = g_vXiaoChuanXmlDoc.getElementsByTagName("result")[0];
    if (xResult.getElementsByTagName("history").length == 0) {
        ShowDisplay("XiaoChuan_list", false)
    }
    else {
        var sHtml = document.body.innerHTML.between("<!--TP.LIST", "TP.LIST-->");
        var mapName = [
				['id', '<%id%>'],
				['value', '<%value%>', function(v) { return getDegreeDesc(v) } ],
				['date', '<%date%>', function(v) { return parseDate(v).toCommonString() } ]
				];
        var iStart = (iPageNo - 1) * iPageNum;
        var iEnd = iStart + iPageNum;
        iEnd = iEnd > iTotal ? iTotal : iEnd;
        showXmlNode(xResult, oParent, "history", sHtml, mapName, iStart, iEnd);
    }
    iTotalPageNum = Math.ceil(iTotal / iPageNum);
    showPage(iPageNo, iTotalPageNum, function(n) { return "javascript:printData(" + n + "," + iPageNum + "," + iTotal + ");"; }, document.getElementById("ID_page"), 0);
}
function formatXiaoChuanFlashData(startDate, endDate, bForce) {
    g_vXiaoChuanStrs = [];
    var xResult = g_vXiaoChuanXmlDoc.getElementsByTagName("result")[0];
    //var iHeight = xResult.getElementsByTagName("height")[0].value;
    var xHistories = xResult.getElementsByTagName("histories")[0];
    var xHistory = xResult.getElementsByTagName("history");
    var bgcolor = '"bg_colour": "#FFFFFF"';
    var now = parseDate(xResult.getAttribute("now"));
    printData(1, 10, xHistory.length);

    if (typeof (endDate) == "undefined") endDate = now;
    if (typeof (startDate) == "undefined") { startDate = parseDate(xResult.getAttribute("diseasestart")); bForce = false; }
    var startValue = 0;
    //format 
    var sTitle = '"title":{"text":"哮喘控制情况","style": "{font-size: 20px;color:#0000ff;font-family:Verdana;text-align:center}"}';
    var sXAxis = '';
    var sElement = '';

    var YAxis = [0, 15, 18, 24, 25];
    var YAxisLabels = ["没有控制", "", "", "部分控制", "完全控制"];
    var sYAxis = '"y_axis":{"font-size":"14px","max":4,"step":1,"labels":["' + YAxisLabels.join('","') + '"] }';
    var curDate = now;
    if (xHistory != null && xHistory.length > 0) {
        curDate = parseDate(xHistory[0].getAttribute("date"));
        if (typeof (bForce) == "undefined" || !bForce) {
            if (curDate < startDate) startDate = curDate;
        }
    }
    var xLabels = [];
    iCur = 4;
    var vXDistance = Math.ceil(distanceDate(startDate, endDate) / (iCur - 1.0)); //alert("distance:"+distanceDate(startDate,endDate)+"unit:"+vXDistance);

    curDate = endDate;
    do {
        xLabels[iCur] = curDate.toCommonString();
        startDate = curDate;
        curDate = getNextDay(curDate, -vXDistance);
    } while (--iCur >= 0);
    iCur = 0;
    var xUnit = (distanceDate(startDate, endDate) + 0.0) / (xLabels.length - 1);
    var sXAxis = '"x_axis":{ "max":' + (xLabels.length - 0.8) + ',"labels":{"rotate":0,"labels":["' + xLabels.join('","') + '"]}}';
    var Elements = [];
    for (var i = 0; i < xHistory.length; ++i) {
        var date = parseDate(xHistory[i].getAttribute("date"));
        if (date < startDate) continue;
        if (date > endDate) break;
        curDistance = distanceDate(startDate, date);
        var yValue = getValueByArray(YAxis, (parseFloat(xHistory[i].getAttribute("value"))));
        var xValue = curDistance / xUnit;

        var sElement = '{"value":' + yValue + ',"colour":"#736AFF","tip":"' + getDegreeDescRange(yValue) + " " + date.toCommonString() + '","x":' + xValue + '}';
        Elements.push(sElement);
    }
    sElements = '"elements":[{"type":"line_dot","text":"控制程度:完全控制建议三个月减量，部分控制建议继续坚持治疗，未控制时建议给版主留言或者是医院就诊","font-size":12,"width":2,"dot-size": 4, "halo-size":1,"values":[' + Elements.join(',') + ']}]';
    //document.write('{'+sTitle+','+sYLegend+','+sXAxis+','+sYAxis+','+sElements+'}');
    g_vXiaoChuanStrs.push('{' + bgcolor + ',' + sXAxis + ',' + sYAxis + ',' + sElements + '}');
}
function showXiaoChuanFlash(id) {
    if (g_vXiaoChuanStrs.length < id)
        return;
    if (0 == id) {
        for (var i = 1; i <= g_vXiaoChuanStrs.length; ++i) {
            var tmp = _$(getXiaoChuanFlash() + i);
            tmp.load(g_vXiaoChuanStrs[i - 1]);
        }
    }
    else {
        var tmp = _$(getXiaoChuanFlash() + id);
        try { tmp.load(g_vXiaoChuanStrs[id - 1]); }
        catch (e) { }
    }

}
function loadXiaoChuanFlashData(id) {
    function OnSucc(xmlDoc) {
        g_vXiaoChuanXmlDoc = xmlDoc;
        formatXiaoChuanFlashData();
        showXiaoChuanFlash(id);
    }
    function OnFail(xmlDoc) {
        alert("获取数据失败");
    }
    if (typeof (g_sXiaoChuanUrl) == "undefined") g_sXiaoChuanUrl = "/Pages/Alpha/XiaoChuanHistory/XiaoChuanHistory.aspx";
    getResultXml(g_sXiaoChuanUrl, OnSucc, OnFail, OnFail);
}

function showXiaoChuanFlashMode(i, dStart, dEnd) {
    var enddate = g_vNow;
    var startdate;
    switch (i) {
        case 1: startdate = getPreBeginMonthDate(enddate, 3); break;
        case 2: startdate = getPreBeginMonthDate(enddate, 6); break;
        case 3: startdate = getPreBeginMonthDate(enddate, 12); break;
        case 4: startdate = getPreBeginMonthDate(enddate, 36); break;
        case 5:
            startdate = dStart < dEnd ? dStart : dEnd;
            enddate = dStart > dEnd ? dStart : dEnd;
            if (startdate == enddate) {
                alert("起始日期和终止日期相同");
                return;
            }
        default: break;
    }
    formatXiaoChuanFlashData(startdate, enddate, true);
    showXiaoChuanFlash(1);
}
